<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 14:11
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\AlbumTemplateAlbumsModel;

/**
 * Class AlbumTemplateAlbumsService
 * @package SDXApi\Service
 */
class AlbumTemplateAlbumsService extends TemplateAbstract
{
	/**
	 * @var AlbumTemplateAlbumsModel
	 */
	protected $albumTemplateAlbumsModel;

	/**
	 * AlbumTemplateAlbumsService constructor.
	 */
	public function __construct()
	{
		$this->albumTemplateAlbumsModel = new AlbumTemplateAlbumsModel();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return bool
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$cacheName = 'album_template_album_id_' . $template['id'];

		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->checkAlbums($template);
			$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
		}

		return $data;
	}


	/**
	 * 处理专辑数据
	 * Created by Jerry
	 * @param array $template
	 * @return bool|mixed
	 */
	protected function checkAlbums($template = array())
	{
		$albums = $this->albumTemplateAlbumsModel->findAlbumByAlbumId($template['id']);

		if (empty($albums)) {
			return false;
		}

		foreach ($albums as $key => $value) {
			$coverList = $this->albumTemplateAlbumsModel->findAlbumFourByAlbumId($value['id']);
			if (empty($coverList)) {
				$albums[$key]['cover_list'] = array();
				continue;
			}

			$albums[$key]['cover_list'] = $coverList;
		}

		$template['album_data'] = $albums;

		return $template;
	}
}